Migo商城2.0 门户首页大广告位显示(3) 十七

Migo商城2.0 门户首页大广告位显示(3) 十七

接上篇:

内容管理的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div data-options="region:'center'" style="padding:5px">
<table class="easyui-datagrid" id="contentList" data-options="toolbar:contentListToolbar,singleSelect:false,collapsible:true,pagination:true,method:'get',pageSize:20,url:'/rest/content',queryParams:{categoryId:0}">
<thead>
<tr>
<th data-options="field:'id',width:30">ID</th>
<th data-options="field:'title',width:120">内容标题</th>
<th data-options="field:'subTitle',width:100">内容子标题</th>
<th data-options="field:'titleDesc',width:120">内容描述</th>
<th data-options="field:'url',width:60,align:'center',formatter:migo.formatUrl">内容连接</th>
<th data-options="field:'pic',width:50,align:'center',formatter:migo.formatUrl">图片</th>
<th data-options="field:'pic2',width:50,align:'center',formatter:migo.formatUrl">图片2</th>
<th data-options="field:'created',width:130,align:'center',formatter:migo.formatDateTime">创建日期</th>
<th data-options="field:'updated',width:130,align:'center',formatter:migo.formatDateTime">更新日期</th>
</tr>
</thead>
</table>
</div>
</div>

查询显示内容列表:

mapper:
1
2
3
4
5
6
7
8
9
10
11
package com.migo.mapper;

import com.migo.pojo.Content;
import tk.mybatis.mapper.common.Mapper;

/**
* Author 知秋
* Created by kauw on 2016/12/3.
*/
public interface ContentMapper extends Mapper<Content> {
}

service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.migo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.migo.pojo.Content;
import com.migo.pojo.EasyUIDataGridResult;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Author 知秋
* Created by kauw on 2016/12/3.
*/
@Service
public class ContentService extends BaseService<Content> {

public EasyUIDataGridResult queryListByCategoryId(Long categoryId, Integer page, Integer rows) {
PageHelper.startPage(page,rows);
Content expample=new Content();
expample.setCategoryId(categoryId);
List<Content> contents = super.queryListByWhere(expample);
PageInfo<Content> pageInfo=new PageInfo<>(contents);
return new EasyUIDataGridResult(pageInfo.getList(),pageInfo.getTotal());

}
}

controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.migo.controller;

import com.migo.pojo.EasyUIDataGridResult;
import com.migo.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Author 知秋
* Created by kauw on 2016/12/3.
*/
@Controller
@RequestMapping("content")
public class ContentController {
private static final Logger logger= LoggerFactory.getLogger(ContentController.class);
@Autowired
private ContentService contentService;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<EasyUIDataGridResult> queryListByCategoryId(
@RequestParam("categoryId") Long categoryId,
@RequestParam(value = "page",defaultValue = "1") Integer page,
@RequestParam(value = "rows",defaultValue = "20") Integer rows

){

try {
if (logger.isInfoEnabled()){
logger.info("根据categoryId查询内容列表 categoryId = {}",categoryId);
}
EasyUIDataGridResult result=this.contentService.queryListByCategoryId(categoryId,page,rows);
return ResponseEntity.ok(result);
} catch (Exception e) {
logger.error("根据categoryId查询内容列表 服务器傲娇了 categoryId = {}",categoryId);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
运行测试结果:

新增内容

先进行校验,通过后访问/rest/page/content-add

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
text:'新增',
iconCls:'icon-add',
handler:function(){
var node = $("#contentCategoryTree").tree("getSelected");
if(!node || !$("#contentCategoryTree").tree("isLeaf",node.target)){
$.messager.alert('提示','新增内容必须选择一个内容分类!');
return ;
}
TT.createWindow({
url : "/rest/page/content-add"
});
}
}

content-add.jsp为了更加适应restful,进行迭代,原代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//提交到后台的RESTful
$.ajax({
type: "POST",
url: "/rest/content",
data: $("#contentAddForm").serialize(),
success: function(msg){
$.messager.alert('提示','新增内容成功!');
$("#contentList").datagrid("reload");
TT.closeCurrentWindow();
},
error: function(){
$.messager.alert('提示','新增内容失败!');
}
});
},
修改后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$.ajax({
type: "POST",
url: "/rest/content",
data: $("#contentAddForm").serialize(),
statusCode : {
201 : function(){
$.messager.alert('提示','新增内容成功!');
$("#contentList").datagrid("reload");
TT.closeCurrentWindow();
},
500 : function(){
$.messager.alert('提示','新增内容失败!');
}
}

});

controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 新增内容
* @param content
* @return
*/

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> add(Content content){

try {
if (logger.isInfoEnabled()){
logger.info("新增内容 contentTitle = {}",content.getTitle());
}
content.setId(null);
this.contentService.save(content);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
logger.error("新增内容 contentTitle = {}",content.getTitle(),e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
运行测试

发现查询内容列表没有按修改时间排序,故重写代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.migo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.migo.mapper.ContentMapper;
import com.migo.pojo.Content;
import com.migo.pojo.EasyUIDataGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static javafx.scene.input.KeyCode.O;

/**
* Author 知秋
* Created by kauw on 2016/12/3.
*/
@Service
public class ContentService extends BaseService<Content> {
@Autowired
private ContentMapper contentMapper;

public EasyUIDataGridResult queryListByCategoryId(Long categoryId, Integer page, Integer rows) {
PageHelper.startPage(page,rows);


Example expample=new Example(Content.class);
expample.createCriteria().andCondition("category_id").andEqualTo(categoryId);
expample.orderBy("updated").desc();
List<Content> contents = contentMapper.selectByExample(expample);
PageInfo<Content> pageInfo=new PageInfo<>(contents);
return new EasyUIDataGridResult(pageInfo.getList(),pageInfo.getTotal());

}
}
运行显示效果:

因进度问题,修改和删除暂时就不实现了,之前有类似代码,很简单,读者可自行解决

对外提供接口服务:

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.migo.controller.webs;

import com.migo.pojo.EasyUIDataGridResult;
import com.migo.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Author 知秋
* Created by kauw on 2016/12/4.
*/
@Controller
@RequestMapping("webs/content")
public class WebsContenController {
private static final Logger logger= LoggerFactory.getLogger(WebsContenController.class);
@Autowired
private ContentService contentService;
/**
* 根据categoryId查询内容列表
* @param categoryId
* @param page
* @param rows
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<EasyUIDataGridResult> queryListByCategoryId(
@RequestParam("categoryId") Long categoryId,
@RequestParam(value = "page",defaultValue = "1") Integer page,
@RequestParam(value = "rows",defaultValue = "20") Integer rows

){

try {
if (logger.isInfoEnabled()){
logger.info("根据categoryId查询内容列表 categoryId = {}",categoryId);
}
EasyUIDataGridResult result=this.contentService.queryListByCategoryId(categoryId,page,rows);
return ResponseEntity.ok(result);
} catch (Exception e) {
logger.error("根据categoryId查询内容列表 服务器傲娇了 categoryId = {}",categoryId,e);

}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}

运行与测试:

您的支持将鼓励我继续创作!